﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYFamilyLibDataManager;

namespace YArchitech.HVAC
{
	public partial class AirPortSelForm : System.Windows.Forms.Form
	{
		public AirPortSelForm(Autodesk.Revit.DB.Document docArgu, double airVolumeArgu)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.MouseDown += this.form_MouseleftClick;
			this.hvacDR = new HVACDataReader(docArgu);
			this.InitCombox();
			this.UserSettingRead();
			this.textvolume.Text = Convert.ToString(airVolumeArgu);
			this.toolTip1.ShowAlways = true;
		}

		private void InitCombox()
		{
			foreach (string item in this.hvacDR.GetFamilySort("风口"))
			{
				this.comType.Items.Add(item);
			}
			this.comType.SelectedIndex = 0;
		}

		private void btnSpace_Click(object sender, EventArgs e)
		{
			this.UserSettingWrite();
			this.strAirPortType = this.comType.SelectedItem.ToString();
			base.DialogResult = DialogResult.Yes;
			base.Close();
		}

		private void btnSel_Click(object sender, EventArgs e)
		{
			this.AirPortShowList.Items.Clear();
			this.airTVolume = Convert.ToDouble(this.textvolume.Text);
			this.airPortNumber = (int)Convert.ToInt16(this.txtNumber.Text);
			double num = this.airTVolume / (double)this.airPortNumber;
			this.curDbFamilyList = this.hvacDR.GetFamilyAllInfoList(this.comType.SelectedItem.ToString());
			new List<DbParameter>();
			if (this.curDbFamilyList == null)
			{
				return;
			}
			foreach (DbFamily dbFamily in this.curDbFamilyList)
			{
				List<string> symbols = dbFamily.Symbols;
				IComparer<string> comparer = new ListSortByNumber();
				symbols.Sort(comparer);
				foreach (string text in symbols)
				{
					foreach (DbParameter dbParameter in this.hvacDR.GetSymbolParameter(dbFamily, text))
					{
						if (dbParameter.ParamDefinition != null && dbParameter.Value != null && "" != dbParameter.Value.ToString().Trim() && dbParameter.ParamDefinition.Name == "风量")
						{
							double num2 = Convert.ToDouble(dbParameter.Value);
							if (num * 0.8 < num2 && num2 < num * 1.2)
							{
								this.AirPortShowList.Items.Add(dbFamily.Name + "\\" + text);
							}
						}
					}
				}
			}
			if (this.AirPortShowList.Items.Count != 0)
			{
				this.AirPortShowList.SelectedItem = this.AirPortShowList.Items[0];
			}
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			this.UserSettingWrite();
			this.strAirPortType = this.comType.SelectedItem.ToString();
			if (this.AirPortShowList.SelectedItem == null)
			{
				this.strFamilyName = "";
				this.strFamilyType = "";
				base.Close();
				return;
			}
			string text = this.AirPortShowList.SelectedItem.ToString();
			int num = text.LastIndexOf('\\');
			this.strFamilyName = text.Remove(num);
			this.strFamilyType = text.Remove(0, num + 1);
			base.Close();
		}

		private void btnClose_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void AirPortShowList_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.AirPortShowList.SelectedItem != null && this.AirPortShowList.SelectedItem.ToString() != "")
			{
				string text = this.AirPortShowList.SelectedItem.ToString();
				int num = text.LastIndexOf('\\');
				string text2 = text.Remove(num);
				string strTypeName = text.Remove(0, num + 1);
				if (this.GetFamilyTypeParameter(text2, strTypeName))
				{
					this.propertyGridAirPort.SelectedObject = this.hyPropertys;
					return;
				}
			}
			else
			{
				MessageBox.Show("请选择族类型", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
			}
		}

		private bool GetFamilyTypeParameter(string strFamilyName, string strTypeName)
		{
			if (this.curDbFamilyList.Count == 0)
			{
				return false;
			}
			DbFamily dbFamily = null;
			foreach (DbFamily dbFamily2 in this.curDbFamilyList)
			{
				if (dbFamily2.Name == strFamilyName)
				{
					dbFamily = dbFamily2;
					break;
				}
			}
			if (dbFamily == null)
			{
				return false;
			}
			if (this.AirPortShowList.SelectedItem != null)
			{
				Image image = dbFamily.Image;
				this.pictureAirPort.Image = image;
				new List<DbParameter>();
				List<DbParameter> symbolParameter = this.hvacDR.GetSymbolParameter(dbFamily, strTypeName);
				this.hyPropertys = new HYPropertys();
				HYProperty[] array = new HYProperty[symbolParameter.Count];
				int num = 0;
				foreach (DbParameter dbParameter in symbolParameter)
				{
					array[num] = new HYProperty();
					if (dbParameter.ParamDefinition != null && dbParameter.Value != null && "" != dbParameter.Value.ToString().Trim())
					{
						array[num].Name = dbParameter.ParamDefinition.Name;
						array[num].Description = dbParameter.ParamDefinition.Name;
						array[num].Value = dbParameter.Value + " " + dbParameter.ParamDefinition.Unit;
						array[num].Category = "参数";
						this.hyPropertys.Add(array[num]);
						num++;
					}
				}
			}
			return true;
		}

		private void txtAirPortV_TextChanged(object sender, EventArgs e)
		{
			if (!this.txtAirPortV.Enabled)
			{
				return;
			}
			this.airTVolume = Convert.ToDouble(this.textvolume.Text);
			this.airPortVolume = Convert.ToDouble(this.txtAirPortV.Text);
			this.airPortNumber = (int)(this.airTVolume / this.airPortVolume);
			if (this.airTVolume % this.airPortVolume != 0.0)
			{
				this.airPortNumber++;
			}
			this.txtNumber.Text = Convert.ToString(this.airPortNumber);
		}

		private void txtNumber_TextChanged(object sender, EventArgs e)
		{
			if (!this.txtNumber.Enabled)
			{
				return;
			}
			this.airTVolume = Convert.ToDouble(this.textvolume.Text);
			this.airPortNumber = (int)Convert.ToInt16(this.txtNumber.Text);
			this.airPortVolume = this.airTVolume / (double)this.airPortNumber;
			this.txtAirPortV.Text = Convert.ToString(this.airPortVolume);
		}

		private void textvolume_TextChanged(object sender, EventArgs e)
		{
			if (this.txtNumber.Enabled)
			{
				this.airTVolume = Convert.ToDouble(this.textvolume.Text);
				this.airPortNumber = (int)Convert.ToInt16(this.txtNumber.Text);
				this.airPortVolume = this.airTVolume / (double)this.airPortNumber;
				this.txtAirPortV.Text = Convert.ToString(this.airPortVolume);
				return;
			}
			this.airTVolume = Convert.ToDouble(this.textvolume.Text);
			this.airPortNumber = (int)Convert.ToInt16(this.txtNumber.Text);
			this.airPortVolume = this.airTVolume / (double)this.airPortNumber;
			this.txtAirPortV.Text = Convert.ToString(this.airPortVolume);
		}

		private bool selSpaceGetAirV(string strType)
		{
			Selection selection = null;
			UIDocument uidocument = null;
			Reference reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, "请选择空间");
			Element element = uidocument.Document.GetElement(reference);
			if (element.GetType() != typeof(Space))
			{
				YJKMessageBox.Information("所选择的不是空间，请重新选择!");
				return false;
			}
			Space space = element as Space;
			if (!(strType == "送风口"))
			{
				if (!(strType == "排风口"))
				{
					if (strType == "回风口")
					{
						this.textvolume.Text = Convert.ToString(space.DesignExhaustAirflow);
					}
				}
				else
				{
					this.textvolume.Text = Convert.ToString(space.DesignReturnAirflow);
				}
			}
			else
			{
				this.textvolume.Text = Convert.ToString(space.DesignSupplyAirflow);
			}
			return true;
		}

		private void UserSettingWrite()
		{
			UserSetting userSetting = new UserSetting();
			userSetting.WriteValue("AirPortSelForm", "AirPortType", this.comType.SelectedItem.ToString());
			if (string.IsNullOrEmpty(this.textvolume.Text))
			{
				this.textvolume.Text = "2000";
			}
			userSetting.WriteValue("AirPortSelForm", "TVolume", this.textvolume.Text);
			if (string.IsNullOrEmpty(this.txtNumber.Text))
			{
				this.txtNumber.Text = "4";
			}
			userSetting.WriteValue("AirPortSelForm", "Number", this.txtNumber.Text);
			if (string.IsNullOrEmpty(this.txtAirPortV.Text))
			{
				this.txtAirPortV.Text = "500";
			}
			userSetting.WriteValue("AirPortSelForm", "SVolume", this.txtAirPortV.Text);
			userSetting.WriteValue("AirPortSelForm", "NumberEnable", this.txtNumber.Enabled.ToString());
			userSetting.WriteValue("AirPortSelForm", "SVolumeEnable", this.txtAirPortV.Enabled.ToString());
		}

		private void UserSettingRead()
		{
			UserSetting userSetting = new UserSetting();
			this.comType.SelectedItem = userSetting.GetValue("AirPortSelForm", "AirPortType");
			this.textvolume.Text = userSetting.GetValue("AirPortSelForm", "TVolume");
			if (string.IsNullOrEmpty(this.textvolume.Text))
			{
				this.textvolume.Text = "2000";
			}
			this.txtNumber.Text = userSetting.GetValue("AirPortSelForm", "Number");
			if (string.IsNullOrEmpty(this.txtNumber.Text))
			{
				this.txtNumber.Text = "4";
			}
			this.txtAirPortV.Text = userSetting.GetValue("AirPortSelForm", "SVolume");
			if (string.IsNullOrEmpty(this.txtAirPortV.Text))
			{
				this.txtAirPortV.Text = "500";
			}
			this.txtNumber.Enabled = Convert.ToBoolean(userSetting.GetValue("AirPortSelForm", "NumberEnable"));
			this.txtAirPortV.Enabled = Convert.ToBoolean(userSetting.GetValue("AirPortSelForm", "SVolumeEnable"));
		}

		private void form_MouseleftClick(object sender, MouseEventArgs e)
		{
			if (e.Button != MouseButtons.Left || e.Clicks < 1)
			{
				return;
			}
			if (e.X >= this.txtNumber.Location.X && e.X <= this.txtNumber.Location.X + this.txtNumber.Size.Width && e.Y >= this.txtNumber.Location.Y && e.Y <= this.txtNumber.Location.Y + this.txtNumber.Size.Height)
			{
				this.txtNumber.Enabled = true;
				this.txtAirPortV.Enabled = false;
			}
			if (e.X >= this.txtAirPortV.Location.X && e.X <= this.txtAirPortV.Location.X + this.txtAirPortV.Size.Width && e.Y >= this.txtAirPortV.Location.Y && e.Y <= this.txtAirPortV.Location.Y + this.txtAirPortV.Size.Height)
			{
				this.txtNumber.Enabled = false;
				this.txtAirPortV.Enabled = true;
			}
		}

		private HVACDataReader hvacDR;

		private double airTVolume;

		private int airPortNumber;

		private double airPortVolume;

		public string strAirPortType;

		public string strFamilyName;

		public string strFamilyType;

		private HYPropertys hyPropertys;

		private List<DbFamily> curDbFamilyList = new List<DbFamily>();
	}
}
